<template>
  <div>
    <el-card class="!border-none reactive el-card-client" shadow="never">
      <template #header>
        <el-row :gutter="10">
          <el-col :span="5">
            <el-input
              class="mr_10 input_style"
              v-model="queryParams.keywords"
              clearable
              placeholder="请输入搜索关键字"
            />
          </el-col>
          <el-col :span="5">
            <el-date-picker
              class="mr_10 input_time_style"
              v-model="queryParams.create_time"
              type="date"
              placeholder="请选择日期"
              clear
            />
          </el-col>
          <el-col :span="5">
            <el-select
              class="input_style mr_10"
              v-model="queryParams.type"
              clearable
              placeholder="请选择类型"
            >
              <el-option
                v-for="(item, index) in dictData.activity_type"
                :key="index"
                :label="item.name"
                :value="item.value"
              />
            </el-select>
          </el-col>
          <el-col :span="7">
            <el-button class="mr_10" type="primary" @click="resetPage">
              <template #icon>
                <el-icon color="#fff">
                  <Search />
                </el-icon> </template
              >搜索
            </el-button>
            <el-button class="mr_10" @click="resetParams">重置</el-button>
            <el-button
              v-perms="['activity.zg_apply/delete']"
              :disabled="!selectData.length"
              @click="handleDelete(selectData)"
              >删除</el-button
            >
          </el-col>
          <!-- <el-col :span="2" class="flex justify-end">
            <move-button></move-button>
          </el-col> -->
        </el-row>
      </template>
      <div>
        <el-table
          :data="pager.lists"
          border
          @selection-change="handleSelectionChange"
          v-loading="pager.loading"
          stripe
          max-height="calc(100vh - 14rem)"
        >
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column
            label="id"
            prop="id"
            show-overflow-tooltip
            align="center"
          />
          <el-table-column label="类型" prop="type" align="center">
            <template #default="{ row }">
              <dict-value
                :options="dictData.activity_type"
                :value="row.type"
                align="center"
              />
            </template>
          </el-table-column>
          <el-table-column
            label="联系电话"
            prop="mobile"
            show-overflow-tooltipalign="center"
          />
          <el-table-column
            label="描述"
            prop="detail"
            show-overflow-tooltip
            align="center"
          />
          <el-table-column
            label="操作"
            width="120"
            fixed="right"
            align="center"
          >
            <template #default="{ row }">
              <el-button
                v-perms="['activity.zg_complaint/edit']"
                type="primary"
                link
                @click="handleEdit(row)"
                >编辑</el-button
              >
              <el-button
                v-perms="['activity.zg_complaint/delete']"
                type="danger"
                link
                @click="handleDelete(row.id)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div>
        <pagination class="position" v-model="pager" @change="getLists" />
      </div>
    </el-card>
    <edit-popup
      v-if="showEdit"
      ref="editRef"
      :dict-data="dictData"
      @success="getLists"
      @close="showEdit = false"
    />
  </div>
</template>

<script lang="ts" setup name="zgComplaintLists">
import { usePaging } from "@/hooks/usePaging";
import { useDictData } from "@/hooks/useDictOptions";
import { apiZgComplaintLists, apiZgComplaintDelete } from "@/api/zg_complaint";
import { timeFormat } from "@/utils/util";
import feedback from "@/utils/feedback";
import EditPopup from "./edit.vue";
import moveButton from "@/components/moveButton/index.vue";

const editRef = shallowRef<InstanceType<typeof EditPopup>>();
// 是否显示编辑框
const showEdit = ref(false);

// 查询条件
const queryParams = reactive({
  keywords: "",
  type: "",
  create_time: "",
});

// 选中数据
const selectData = ref<any[]>([]);

// 表格选择后回调事件
const handleSelectionChange = (val: any[]) => {
  selectData.value = val.map(({ id }) => id);
};

// 获取字典数据
const { dictData } = useDictData("activity_type");

// 分页相关
const { pager, getLists, resetParams, resetPage } = usePaging({
  fetchFun: apiZgComplaintLists,
  params: queryParams,
});

// 添加
const handleAdd = async () => {
  showEdit.value = true;
  await nextTick();
  editRef.value?.open("add");
};

// 编辑
const handleEdit = async (data: any) => {
  showEdit.value = true;
  await nextTick();
  editRef.value?.open("edit");
  editRef.value?.setFormData(data);
};

// 删除
const handleDelete = async (id: number | any[]) => {
  await feedback.confirm("确定要删除？");
  await apiZgComplaintDelete({ id });
  getLists();
};

getLists();
</script>
<style lang="scss" scoped>
.flex {
  display: flex !important;
}
</style>
